package com.taofuxn.yycreazy.ui.activity;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.taofuxn.yycreazy.MyApp;
import com.taofuxn.yycreazy.R;
import com.taofuxn.yycreazy.adpater.CollectionAdapter;
import com.taofuxn.yycreazy.database.dao.DaoSession;
import com.taofuxn.yycreazy.database.model.Collection;
import com.taofuxn.yycreazy.mvp.BasePresenter;
import com.taofuxn.yycreazy.mvp.MvpActivity;
import com.taofuxn.yycreazy.mvp.presenter.CollectionPresenter;
import com.taofuxn.yycreazy.mvp.view.ICollectView;
import com.taofuxn.yycreazy.ports.ITaofuDeleListener;
import com.taofuxn.yycreazy.util.MyLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

public class PerConllectionActivity extends MvpActivity implements ICollectView, ITaofuDeleListener {

    @InjectView(R.id.collectionRecyclerview)
    RecyclerView ccRecyclerview;

    private CollectionAdapter mAdapter;
    private ArrayList<Collection> mDatas;

    private CollectionPresenter mCollectionPresenter;


    @Override
    protected int setContentLayout() {
        return R.layout.activity_per_conllection;
    }

    @Override
    protected void init() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        toolbar.setNavigationIcon(R.mipmap.abc_ic_ab_back_mtrl_am_alpha);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
            }
        });

        mDatas = new ArrayList<>();
        mAdapter = new CollectionAdapter(R.layout.item_collection, mDatas);
        ccRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        ccRecyclerview.setAdapter(mAdapter);
        mAdapter.setmITaofuDeleListener(this);


    }

    @Override
    protected BasePresenter createDefaultPresenter() {
        mCollectionPresenter = new CollectionPresenter(this);
        mDatas.addAll(mCollectionPresenter.getCollectionDate());
        MyLog.showLog(mCollectionPresenter.getCollectionDate().size());
        mAdapter.notifyItemRangeChanged(0, mDatas.size());
        return mCollectionPresenter;
    }

    /**
     * 执行删除操作
     * @param position
     */
    @Override
    public void deleteItem(int position) {
        mCollectionPresenter.deleteCollectionByEntity(mDatas.get(position));
        mDatas.remove(position);
        mAdapter.notifyItemRemoved(position);
        mAdapter.notifyItemRangeChanged(position,mDatas.size());
    }
}
